Method Reference কি এবং এর ব্যবহার

Method References - জাভা ফাংশনাল প্রোগ্রামিং (Java Functional Programming) - Java Technologies

333

Method Reference হল Java 8-এর একটি গুরুত্বপূর্ণ ফিচার, যা Lambda Expressions এর একটি সংক্ষিপ্ত রূপ। Method Reference ব্যবহার করে, আপনি একটি মেথডকে সরাসরি ইন্টারফেসের সাথে সম্পর্কিত একটি ল্যাম্বডা এক্সপ্রেশন হিসেবে ব্যবহার করতে পারেন, যা কোডকে আরও সংক্ষিপ্ত এবং পাঠযোগ্য করে তোলে।

Lambda expressions এর সাথে মেথড রেফারেন্স ব্যবহার করা কোডের রিডেবিলিটি এবং পরিচ্ছন্নতা বাড়ায়, কারণ এটি ল্যাম্বডা এক্সপ্রেশনের পরিবর্তে শুধুমাত্র মেথডের রেফারেন্স দেয়।


Method Reference এর রূপ:

Method Reference এর সাধারণ সিনট্যাক্স হল:

ClassName::methodName

এখানে:

  • ClassName: যেই ক্লাস বা অবজেক্টের মেথড আপনি রেফারেন্স করতে চান।
  • methodName: যেই মেথডটি আপনি কল করতে চান।

Method Reference তিন ধরনের হতে পারে:

  1. Static Method Reference
  2. Instance Method Reference (from a particular object)
  3. Instance Method Reference (from an arbitrary object of a particular type)

Method Reference Types এবং তাদের ব্যবহার:

1. Static Method Reference:

এটি একটি স্ট্যাটিক মেথডকে রেফারেন্স করার জন্য ব্যবহৃত হয়। স্ট্যাটিক মেথডের নামের আগে ক্লাসের নাম ব্যবহার করা হয়।

Syntax:

ClassName::staticMethodName

Example:

import java.util.Arrays;
import java.util.List;

public class MethodReferenceExample {
    public static void printUpperCase(String s) {
        System.out.println(s.toUpperCase());
    }

    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        // Using method reference to call static method
        names.forEach(MethodReferenceExample::printUpperCase);
    }
}

Output:

ALICE
BOB
CHARLIE

এখানে, MethodReferenceExample::printUpperCase একটি স্ট্যাটিক মেথড রেফারেন্স যা forEach মেথডে ব্যবহার করা হয়েছে।


2. Instance Method Reference (from a particular object):

এটি একটি নির্দিষ্ট অবজেক্টের মেথড রেফারেন্স করতে ব্যবহৃত হয়। এই রেফারেন্সের মধ্যে আপনি একটি অবজেক্টের মেথডকে সরাসরি কল করতে পারেন।

Syntax:

instanceName::instanceMethodName

Example:

import java.util.Arrays;
import java.util.List;

public class MethodReferenceExample {
    public void printUpperCase(String s) {
        System.out.println(s.toUpperCase());
    }

    public static void main(String[] args) {
        MethodReferenceExample obj = new MethodReferenceExample();
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        // Using method reference to call instance method
        names.forEach(obj::printUpperCase);
    }
}

Output:

ALICE
BOB
CHARLIE

এখানে, obj::printUpperCase একটি instance method reference যা অবজেক্ট obj এর মেথড printUpperCase কল করেছে।


3. Instance Method Reference (from an arbitrary object of a particular type):

এটি একটি অ্যানোনিমাস অবজেক্টের মেথড রেফারেন্স করতে ব্যবহৃত হয়। এটি সাধারণত lambda expressions এ ব্যবহৃত হয় এবং আপনি যেকোনো অবজেক্টের মেথডকে কল করতে পারেন।

Syntax:

ClassName::instanceMethodName

Example:

import java.util.Arrays;
import java.util.List;

public class MethodReferenceExample {
    public void printUpperCase(String s) {
        System.out.println(s.toUpperCase());
    }

    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        // Using method reference to call instance method from arbitrary object
        names.forEach(new MethodReferenceExample()::printUpperCase);
    }
}

Output:

ALICE
BOB
CHARLIE

এখানে, new MethodReferenceExample()::printUpperCase একটি instance method reference যা কোনো নির্দিষ্ট অবজেক্টের মাধ্যমে মেথড কল করেছে।


Method Reference এবং Lambda Expressions এর মধ্যে পার্থক্য:

FeatureLambda ExpressionMethod Reference
Syntax(parameters) -> expressionClassName::methodName or object::methodName
ConcisenessLonger syntax compared to method references.Shorter and more readable syntax when referencing existing methods.
UsageSuitable when you need to define new logic inline.Suitable when referring to existing methods (static, instance).
Example(String s) -> System.out.println(s.toUpperCase())String::toUpperCase
When to useUse when writing new logic inline.Use when referencing an already defined method.

Lambda Expression vs Method Reference Example:

Let's see how a lambda expression can be converted into method reference.

Using Lambda Expression:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// Using lambda expression
names.forEach(name -> System.out.println(name.toUpperCase()));

Using Method Reference:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// Using method reference
names.forEach(String::toUpperCase);

In this case, the method reference String::toUpperCase is equivalent to the lambda expression (name -> System.out.println(name.toUpperCase())).


Advantages of Method References:

  1. Conciseness: Method references are more concise than lambda expressions and make the code more readable.
  2. Cleaner Code: When you're calling an existing method, method references can simplify the code and eliminate unnecessary lambda expressions.
  3. Improved Readability: It’s easier to understand the intent of code with method references since you're directly referencing an existing method.
  4. Reusability: Method references promote reusability because they reference methods that are already implemented elsewhere.

Method Reference in Java provides a more concise and readable alternative to lambda expressions when you need to refer to existing methods. It fits perfectly with the functional programming style in Java, allowing for cleaner, easier-to-read code. Method references can be used for static methods, instance methods, and arbitrary object methods, providing flexibility and improving code quality. Use method references when you want to refer to an existing method, and lambda expressions when you need to define custom logic inline.

Content added By
Promotion

Are you sure to start over?

Loading...